package com.huarenkeji.china_push.core;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import android.widget.Toast;


import com.meizu.cloud.pushsdk.MzPushMessageReceiver;
import com.meizu.cloud.pushsdk.handler.MzPushMessage;
import com.meizu.cloud.pushsdk.platform.message.*;

import com.huarenkeji.china_push.PushManager;
import com.huarenkeji.china_push.util.Logger;

import java.util.HashMap;
import java.util.Map;

public class MeiZuPushInterface implements PushInterface {
    private static final String TAG = "MeiZuPushInterface";

    public static final String MANUFACTURER = "MEIZU";
    public static final String MEI_ZU_APP_KEY = "MEI_ZU_APP_KEY";
    public static final String MEI_ZU_APP_ID = "MEI_ZU_APP_ID";

    public Context meiZuPushInterfaceContent;

    @Override
    public void init(Context context, Bundle bundle) {
        try {
            meiZuPushInterfaceContent = context;
            String appKey = bundle.getString(MEI_ZU_APP_KEY);
            String appId = bundle.getString(MEI_ZU_APP_ID);
            Logger.i(TAG, "初始化魅族方法 appKey:" + appKey + "appId: " + appId);
            if (TextUtils.isEmpty(appKey) || TextUtils.isEmpty(appId)) {
                String errorMsg = "OPPO 初始化失败，未找到appKey或appSecret 请在build.gradle中配置MEI_ZU_APP_KEY和MEI_ZU_APP_ID";
                Logger.e(errorMsg);
                PushManager.onRegisterFail(errorMsg);
                return;
            }
            // com.meizu.cloud.pushsdk.PushManager.register(context, appId, appKey);
            com.meizu.cloud.pushsdk.PushManager.register(context, appId, appKey);
        } catch (Exception e) {
            Logger.i(TAG, "init魅族方法Fail 失败， 错误信息error:" + e.getMessage());
        }
        
    }

    @Override
    public void processIntent(@NonNull Intent intent) {
        Bundle bundle = intent.getExtras();
        // Toast.makeText(meiZuPushInterfaceContent, "Intent: " + intent + " OR " + bundle, Toast.LENGTH_LONG).show();
        if (bundle != null) {
            Logger.i("通知点击3");
            Map<String, Object> pushContetMap = new HashMap<>();
            for (String key : bundle.keySet()) {
                String content = bundle.getString(key);
                pushContetMap.put(key, content);
                Logger.i("receive data from push, key = " + key + ", content = " + content);
            }
            // Toast.makeText(meiZuPushInterfaceContent, "pushContetMap: " + pushContetMap, Toast.LENGTH_LONG).show();
            // if(!pushContetMap.containsKey("profile"))  {
            //     PushManager.onOpenNotification(pushContetMap);
            // }
            if(pushContetMap.containsKey("profile") || pushContetMap.containsKey("FLAG_PAYLOAD_FROM_WECHAT") || pushContetMap.containsKey("KEY_FLUWX_EXTRA"))  {
                return;
            }
            // PushManager.onOpenNotification(pushContetMap);

//            pushContetMap['platform_extra'].containsKey("task_id")
            if(
                pushContetMap.containsKey("task_id") || 
                pushContetMap.containsKey("platform_extra")
            )  {
                PushManager.onOpenNotification(pushContetMap);
            }
        } else {
            Logger.e("HonorPushInterface processIntent fail bundle is Null");
        }
    }

    @Override
    public boolean supportPush(Context context, Bundle bundle) {
        String appKey = bundle.getString(MEI_ZU_APP_KEY);
        String appId = bundle.getString(MEI_ZU_APP_ID);
        // return !TextUtils.isEmpty(appKey) || !TextUtils.isEmpty(appId) && PushClient.getInstance(context).isSupport();
        return !TextUtils.isEmpty(appKey) || !TextUtils.isEmpty(appId);

    }

    @Override
    public String getManufacturer() {
        return MANUFACTURER;
    }


}
